﻿using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Routing;
using MSAcademic.v2.Web.App_Start;
using StructureMap;
using MSAcademic.v2.Infrastructure;

namespace MSAcademic.v2.Web
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801
    public class MvcApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            _registerDependencies();
            WebApiConfig.Register(GlobalConfiguration.Configuration);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);

            _registerDependencies();
        }

        private void _registerDependencies() 
        {
            ModelBinders.Binders.DefaultBinder = new StructureMapModelBinder();

            ObjectFactory.Configure(x => 
            {
                x.For<HttpContextBase>().HttpContextScoped().Use(() => new HttpContextWrapper(HttpContext.Current));
                x.For<HttpServerUtilityBase>().HttpContextScoped().Use(() => new HttpServerUtilityWrapper(HttpContext.Current.Server));

#if DEBUG
                x.AddRegistry<DebugDependencyRegistry>();
                ControllerBuilder.Current.SetControllerFactory(new StructureMapControllerFactory());
#else
                x.AddRegistry<ProductionDependencyRegistry>();
#endif
            });
            ControllerBuilder.Current.SetControllerFactory(new StructureMapControllerFactory());
        }
      
        
    }
}